﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 地接客 分流订单
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-31 15:25
    public class CBizDiversionOrder : CustBizObject
    {
        private CDmDjkDiversionOrder m_CDmDjkDiversionOrder = new CDmDjkDiversionOrder();
        private readonly List<CDmDjkDiversionOrder> _allAreaInfoList = null;
        private CDmDjkDiversionOrder oDmDjkDiversionOrder
        {
            get { return m_CDmDjkDiversionOrder; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDiversionOrder()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkDiversionOrder.DJK_DIVERSION_ORDER);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["DIVERSION_ORDER_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {

            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }


        /// <summary>
        /// 添加新订单信息
        /// </summary>
        /// <param name="vehicleId">车辆Id</param>
        /// <param name="tName">买家姓名</param>
        /// <param name="tGender">买家性别</param>
        /// <param name="orderFee">定金金额</param>
        /// <param name="totalFee">总订单金额</param>
        /// <param name="orderDt">下单时间</param>
        /// <param name="startTime">开始服务时间</param>
        /// <param name="stopTime">结束服务时间</param>
        /// <param name="tPhone">买家电话</param>
        /// <param name="orderSource">订单来源</param>
        /// <param name="outOrderCode">外部订单号</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-9-2 11:00
        public string[] AddNewOrderInfo(string vehicleId, string tName, string tGender, decimal orderFee, decimal totalFee, DateTime orderDt, DateTime startTime, DateTime stopTime, string tPhone, string orderSource, string outOrderCode, string expire)
        {
            if (string.IsNullOrWhiteSpace(vehicleId))
            {
                return new string[] { "FALSE", "车辆信息有误" };
            }

            if (string.IsNullOrWhiteSpace(tName))
            {
                return new string[] { "FALSE", "买家姓名不能为空" };
            }

            if (string.IsNullOrWhiteSpace(tGender))
            {
                return new string[] { "FALSE", "买家性别不能为空" };
            }

            if (string.IsNullOrWhiteSpace(tPhone))
            {
                return new string[] { "FALSE", "买家联系方式不能为空" };
            }

            if (string.IsNullOrWhiteSpace(outOrderCode))
            {
                return new string[] { "FALSE", "外部订单号不能为空" };
            }

            if (orderFee <= 0M)
            {
                return new string[] { "FALSE", "定金金额必须大于零" };
            }

            if (totalFee <= 0M
                || totalFee <= orderFee)
            {
                return new string[] { "FALSE", "订单总金额必须大于定金金额" };
            }

            if (startTime >= stopTime)
            {
                return new string[] { "FALSE", "服务开始时间必须小于结束时间" };
            }

            DataSet dsVehicle, dsDjack;
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo objCBizVehicleInfo = new CBizVehicleInfo())
            {
                dsVehicle = objCBizVehicleInfo.SelectById(vehicleId);
            }

            if (dsVehicle == null
                || dsVehicle.Tables[0].Rows.Count <= 0)
            {
                return new string[] { "FALSE", "该车辆信息不存在" };
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new CBizCustomerBaseInfo())
            {
                dsDjack = objCBizCustomerBaseInfo.SelectById(dsVehicle.Tables[0].Rows[0]["DJACK_ID"]);
            }

            if (dsDjack == null
                || dsDjack.Tables[0].Rows.Count <= 0)
            {
                return new string[] { "FALSE", "该地接客信息不存在" };
            }

            DataSet dsDvi = this.DataObj.QueryBySQL(@"SELECT COUNT(1) ORDER_COUNT FROM " + this.TableName + @" WHERE ORDER_PLATFORM_CODE='" + outOrderCode + "'");
            if (Convert.ToInt32(dsDvi.Tables[0].Rows[0]["ORDER_COUNT"]) > 0)
            {
                return new string[] { "FALSE", "该订单已存在，请勿重复添加" };
            }

            DataSet ds = new CDmDjkDiversionOrder();
            DataRow dr = ds.Tables[0].NewRow();

            dr["DIVERSION_ORDER_ID"] = this.DataObj.NewID();
            dr["ORDER_CODE"] = this.dtCurrDateTime.ToString("yyyyMMddhhmm") + msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("ORDER_CODE", "", 4); ;
            dr["VEHICLE_ID"] = vehicleId;
            dr["TOURIST_NAME"] = tName;
            dr["TOURIST_GENDER"] = tGender == "M" ? 0 : 1;
            dr["ORDER_DEPOSIT"] = orderFee;
            dr["ORDER_TOTAL_FEE"] = totalFee;
            dr["ORDER_TIME"] = orderDt;
            dr["ORDER_START_TIME"] = startTime;
            dr["ORDER_STOP_TIME"] = stopTime;
            dr["TOURIST_PHONE"] = tPhone;
            dr["ORDER_STATUS"] = 1;
            dr["ORDER_CREATE_USERID"] = msBsc.SvRef.UserInfo.Operator;
            dr["ORDER_UPDATE_USERID"] = msBsc.SvRef.UserInfo.Operator;
            dr["ORDER_UPDATE_TIME"] = this.dtCurrDateTime;
            dr["IS_DELETE"] = 0;
            dr["ORDER_SOURCE_PLATFORM"] = orderSource;
            dr["ORDER_PLATFORM_CODE"] = outOrderCode;
            dr["DJACK_NAME"] = dsDjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_NAME"];
            dr["DJACK_PHONE"] = dsDjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_MOBILE"];
            dr["DJACK_ID"] = dsDjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_ID"];
            dr["ORDER_VEHICLE_NAME"] = dsVehicle.Tables[0].Rows[0]["VEHICLE_NAME"];
            dr["ORDER_VEHICLE_NO"] = dsVehicle.Tables[0].Rows[0]["LICENSE_PLATE_NUMBER"];
            dr["ORDER_EXPIRE"] = expire;
            ds.Tables[0].Rows.Add(dr);

            using (CBizVehicleSchedule objCBizVehicleSchedule = new CBizVehicleSchedule())
            {
                if (objCBizVehicleSchedule.AddVehicleSchedule(dsVehicle.Tables[0].Rows[0]["VEHICLE_IFNO_ID"].ToString(), "已经预定", "已经预定", startTime.ToString("yyyy-MM-dd hh:mm:ss"), stopTime.ToString("yyyy-MM-dd hh:mm:ss"), "#0072E3", msBsc.SvRef.UserInfo.Operator, 0)[0] == "true")
                {
                    this.Update(ds);
                }
            }

            return new string[] { "TRUE", "添加成功" };
        }

        /// <summary>
        /// 根据条件查询订单信息
        /// </summary>
        /// <param name="orderCode">订单编号</param>
        /// <param name="outerCode">外部平台订单编号</param>
        /// <param name="djackName">地接客姓名</param>
        /// <param name="djackPhone">地接客电话</param>
        /// <param name="vehicleCode">车牌号</param>
        /// <param name="isDel">是否已删除</param>
        /// <param name="orderSource">订单来源</param>
        /// <param name="orderStatuss">订单状态</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-9-27 15:00
        public DataSet GetOrderInfoByCondition(string orderCode, string outerCode, string djackName, string djackPhone, string vehicleCode, string orderSource, string orderStatuss, bool isDel, string startTime, string stopTime)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("orderCode", ParameterDirection.Input, EDbType.String, 36, orderCode));
            parmCollection.Add(new CDbParm("outerCode", ParameterDirection.Input, EDbType.String, 36, outerCode));
            parmCollection.Add(new CDbParm("djkName", ParameterDirection.Input, EDbType.String, 36, djackName));
            parmCollection.Add(new CDbParm("djkPhone", ParameterDirection.Input, EDbType.String, 36, djackPhone));
            parmCollection.Add(new CDbParm("isDel", ParameterDirection.Input, EDbType.Boolean, 1, isDel));
            parmCollection.Add(new CDbParm("startTime", ParameterDirection.Input, EDbType.String, 25, startTime));
            parmCollection.Add(new CDbParm("stopTime", ParameterDirection.Input, EDbType.String, 25, stopTime));
            parmCollection.Add(new CDbParm("orderSource", ParameterDirection.Input, EDbType.Integer, 1, orderSource));
            parmCollection.Add(new CDbParm("orderStatus", ParameterDirection.Input, EDbType.Integer, 1, orderStatuss));
            parmCollection.Add(new CDbParm("vehicleCode", ParameterDirection.Input, EDbType.String, 36, vehicleCode));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_DIVERSION_ORDER_FOR_VIEW", parmCollection);
            ds.Tables[0].TableName = this.TableName;

            return ds;
        }


        /// <summary>
        /// 根据条件代理查询订单信息
        /// </summary>
        /// <param name="orderCode">订单编号</param>
        /// <param name="outerCode">外部平台订单编号</param>
        /// <param name="djackName">地接客姓名</param>
        /// <param name="djackPhone">地接客电话</param>
        /// <param name="vehicleCode">车牌号</param>
        /// <param name="orderSource">订单来源</param>
        /// <param name="orderStatuss">订单状态</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-9-7 15:00
        public DataSet GetAgentOrderByCondition(string orderCode, string outerCode, string djackName, string djackPhone, string vehicleCode, string orderSource, string orderStatuss, string startTime, string stopTime)
        {
            #region 确认只能查询3个月以内的数据
            DateTime startDT, stopDT;
            if (string.IsNullOrWhiteSpace(startTime)
                || !DateTime.TryParse(startTime, out startDT))
            {
                startDT = this.dtCurrDateTime.AddMonths(-3);
            }

            if (string.IsNullOrWhiteSpace(stopTime)
                || !DateTime.TryParse(stopTime, out stopDT))
            {
                stopDT = this.dtCurrDateTime;
            }

            TimeSpan dtTs = this.dtCurrDateTime - startDT;

            if (dtTs.Days > 90)
            {
                startDT = this.dtCurrDateTime.AddMonths(-3);
            }

            if (stopDT < startDT)
            {
                stopDT = this.dtCurrDateTime;
            }

            startTime = startDT.ToString("yyyy-MM-dd hh:mm:ss");
            stopTime = stopDT.ToString("yyyy-MM-dd hh:mm:ss");
            #endregion

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("orderCode", ParameterDirection.Input, EDbType.String, 36, orderCode));
            parmCollection.Add(new CDbParm("outerCode", ParameterDirection.Input, EDbType.String, 36, outerCode));
            parmCollection.Add(new CDbParm("djkName", ParameterDirection.Input, EDbType.String, 36, djackName));
            parmCollection.Add(new CDbParm("djkPhone", ParameterDirection.Input, EDbType.String, 36, djackPhone));
            parmCollection.Add(new CDbParm("startTime", ParameterDirection.Input, EDbType.String, 25, startTime));
            parmCollection.Add(new CDbParm("stopTime", ParameterDirection.Input, EDbType.String, 25, stopTime));
            parmCollection.Add(new CDbParm("agentId", ParameterDirection.Input, EDbType.String, 36, msBsc.SvRef.UserInfo.Operator));
            parmCollection.Add(new CDbParm("orderSource", ParameterDirection.Input, EDbType.Integer, 1, orderSource));
            parmCollection.Add(new CDbParm("orderStatus", ParameterDirection.Input, EDbType.Integer, 1, orderStatuss));
            parmCollection.Add(new CDbParm("vehicleCode", ParameterDirection.Input, EDbType.String, 36, vehicleCode));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_AGENT_ORDER_FOR_VIEW", parmCollection);
            ds.Tables[0].TableName = this.TableName;

            return ds;
        }

        /// <summary>
        /// 根据条件查询地接客订单信息
        /// </summary>
        /// <param name="orderCode">订单编号</param>
        /// <param name="orderStatus">订单状态</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-9-14 10:00
        public DataSet GetDjackOrderByCondition(string orderCode, string orderStatus, string startTime, string stopTime)
        {
            #region 确认只能查询3个月以内的数据
            DateTime startDT, stopDT;
            if (string.IsNullOrWhiteSpace(startTime)
                || !DateTime.TryParse(startTime, out startDT))
            {
                startDT = this.dtCurrDateTime.AddMonths(-3);
            }

            if (string.IsNullOrWhiteSpace(stopTime)
                || !DateTime.TryParse(stopTime, out stopDT))
            {
                stopDT = this.dtCurrDateTime;
            }

            TimeSpan dtTs = this.dtCurrDateTime - startDT;

            if (dtTs.Days > 90)
            {
                startDT = this.dtCurrDateTime.AddMonths(-3);
            }

            if (stopDT < startDT)
            {
                stopDT = this.dtCurrDateTime;
            }

            startTime = startDT.ToString("yyyy-MM-dd hh:mm:ss");
            stopTime = stopDT.ToString("yyyy-MM-dd hh:mm:ss");
            #endregion

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("orderCode", ParameterDirection.Input, EDbType.String, 36, orderCode));
            parmCollection.Add(new CDbParm("startTime", ParameterDirection.Input, EDbType.String, 25, startTime));
            parmCollection.Add(new CDbParm("stopTime", ParameterDirection.Input, EDbType.String, 25, stopTime));
            parmCollection.Add(new CDbParm("djkId", ParameterDirection.Input, EDbType.String, 36, msBsc.SvRef.UserInfo.Operator));
            parmCollection.Add(new CDbParm("orderStatus", ParameterDirection.Input, EDbType.Integer, 1, orderStatus));


            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_AGENT_DJACK_ORDER_FOR_VIEW", parmCollection);
            ds.Tables[0].TableName = this.TableName;

            return ds;
        }
    }
}
